import {getAlgorithmList} from "@/api/api";

export const algorithmConfig = {
    data() {
        return {
            algorithmList:[],//算法列表
            algorithmIdList:[],//算法id列表
            algorithmTypeList:[],//算法类型列表
            algorithmModal:false,//算法弹框
            algorithmId:[],//算法id
        }
    },
    mounted() {
        this.getAlgorithmListFn();
    },
    methods: {
        //根据算法type 匹配图片
        getUrl(id){
            let arr = [...this.algorithmTypeList];
            for (let i = 0; i < arr.length; i++) {
                if (id == arr[i].id) {
                    return arr[i].iconUrl
                }
            }
        },
        //请求算法类型
        getAlgorithmListFn(){
            let params = {
                state :1,
                keyword :'',
            }
            getAlgorithmList(params).then(async res=>{
                if(res.code == 200) {
                    this.algorithmTypeList = [];
                    let list = await res.data.list;
                    list.forEach(v => {
                        let obj = {
                            id:v.type,
                            name:v.name,
                            iconUrl:v.iconUrl,
                        }
                         this.algorithmTypeList.push(obj);
                    })
                } else {
                    this.$Message.warning(res.msg);
                }

            }).catch(error=>{
                console.log(error)
            })
        },
        //设置算法类型
        setAlgorithm(id){
            if(this.algorithmId.includes(id)){
                this.algorithmId.forEach((v,i)=>{
                    if(v == id){
                        this.algorithmId.splice(i,1);
                    }
                })
            }else{
                this.algorithmId.push(id);
            }
        },
        //删除算法类型
        delAlgorithm(index,list){
            list.splice(index,1);
            this.algorithmList = list;
            this.algorithmId.splice(index,1);
        },
        //添加到列表
        setAlgorithmBtn(){
            this.algorithmList = [];
            this.algorithmTypeList.forEach(v => {
                this.algorithmId.forEach(item => {
                    if(v.id == item){
                        let obj = {
                            type : v.id,
                            name : v.name,
                            iconUrl : v.iconUrl,
                        }
                        this.algorithmList.push(obj)
                    }
                })
            })

            this.algorithmModal = false;
        },
    },
}
